//
//  LabelDisclosureIndicatorView.m
//  FadeSDWebImage
//
//  Created by 冷悦 on 16/3/10.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "LabelDisclosureIndicatorView.h"
#import "Masonry.h"
@interface LabelDisclosureIndicatorView()

@property (nonatomic, strong) UILabel *leftLabel;

@property (nonatomic, strong) DisclosureIndicatorView *disclosureIndicatorView;

@end

@implementation LabelDisclosureIndicatorView

#pragma mark - Initialize
- (instancetype)init{
    if (self = [super init]) {
        [self setupConstraint];
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self
                                                                                              action:@selector(didTouched:)];
        [self addGestureRecognizer:tapGestureRecognizer];
        self.dir = Bottom;
    }
    
    return self;
}

#pragma mark - Event Response

- (void)didTouched:(UITapGestureRecognizer *)tapGestureRecognizer{
    
    if (self.dir == Bottom) {
        self.dir = Top;
        self.leftLabel.text = self.topText;
    } else if(self.dir == Top){
        self.dir = Bottom;
        self.leftLabel.text = self.bottomText;
    }
    if (self.delegate!=nil&&[self.delegate respondsToSelector:@selector(disclosureIndicatorChangeEvent:)]) {
        [self.delegate disclosureIndicatorChangeEvent:self.dir];
    }
}

#pragma mark - Privates
- (void)setupConstraint{
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.right.equalTo(self.disclosureIndicatorView.mas_left);
    }];
    [self.disclosureIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self);
        make.centerY.equalTo(self.leftLabel);
        make.width.equalTo(self.mas_height).multipliedBy(0.8);
        make.height.equalTo(self.disclosureIndicatorView.mas_width);
    }];
}

- (void)updateUI{
    self.disclosureIndicatorView.direction = self.dir;
    
    switch (self.dir) {
        case Top:{
            [self.disclosureIndicatorView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self);
                make.centerY.equalTo(self);
                make.width.equalTo(self.mas_height).multipliedBy(0.8);
                make.height.equalTo(self.disclosureIndicatorView.mas_width);
            }];
            break;
        }
        case Left:{
            [self.disclosureIndicatorView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self);
                make.centerY.equalTo(self);
                make.width.equalTo(self.mas_height).multipliedBy(0.8);
                make.height.equalTo(self);
            }];
            break;
        }
        case Bottom:{
            [self.disclosureIndicatorView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self);
                make.centerY.equalTo(self);
                make.width.equalTo(self.mas_height).multipliedBy(0.8);
                make.height.equalTo(self.disclosureIndicatorView.mas_width);
            }];
            break;
        }
        case Right:{
            [self.disclosureIndicatorView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self);
                make.centerY.equalTo(self);
                make.width.equalTo(self.mas_height).multipliedBy(0.8);
                make.height.equalTo(self);
            }];
            break;
        }
            
        default:
            break;
    }
}

#pragma mark - Getters
- (UILabel *)leftLabel {
    if (_leftLabel == nil) {
        _leftLabel = [UILabel new];
        _leftLabel.textAlignment = NSTextAlignmentRight;
        _leftLabel.font = [UIFont boldSystemFontOfSize:18];
        [self addSubview:_leftLabel];
    }
    
    return _leftLabel;
}

- (DisclosureIndicatorView *)disclosureIndicatorView{
    if (_disclosureIndicatorView == nil) {
        _disclosureIndicatorView = [DisclosureIndicatorView new];
        _disclosureIndicatorView.backgroundColor = [UIColor clearColor];
        _disclosureIndicatorView.disclosureIndicatorColor = [UIColor colorWithRed:52/255.0 green:152/255.0 blue:219/255.0 alpha:1.0];
        _disclosureIndicatorView.disclosureIndicatorEnable = YES;
        [self addSubview:_disclosureIndicatorView];
    }
    
    return _disclosureIndicatorView;
}

#pragma mark Setters
- (void)setDir:(DisclosureIndicatorViewDir)dir{
    _dir = dir;
    self.disclosureIndicatorView.direction = _dir;
    [self updateUI];
}

- (void)setBottomText:(NSString *)bottomText{
    _bottomText = bottomText;
    
    if (self.dir == Bottom) {
        self.leftLabel.text = _bottomText;
    }
}

- (void)setTopText:(NSString *)topText{
    _topText = topText;
    
    if (self.dir == Top) {
        self.leftLabel.text = _topText;
    }
}

-(void)setRightText:(NSString *)rightText{
    _rightText = rightText;
    
    if (self.dir == Right) {
        self.leftLabel.text = rightText;
    }
}

- (void)setLabelFont:(UIFont *)labelFont{
    self.leftLabel.font = labelFont;
}

- (void)setTextColor:(UIColor *)textColor{
    self.leftLabel.textColor = textColor;
}

- (void)setIndicatorColor:(UIColor *)indicatorColor{
    self.disclosureIndicatorView.disclosureIndicatorColor = indicatorColor;
}

@end
